package com.example.bober.badugi_server;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;


public class Gra extends Activity {

    private Socket gniazdo;
    private ObjectOutputStream strumienWyjsciowy;
    private ObjectInputStream strumienWejsciowy;
    private int ID;

    static TextView TY;
    static TextView Gracz1;
    static TextView Gracz2;
    static TextView Gracz3;
    static TextView Gracz4;
    static TextView Gracz5;

    static LinearLayout lin1;
    LinearLayout lin2;
    LinearLayout lin3;
    LinearLayout lin4;
    LinearLayout lin5;

    LinearLayout r1;
    LinearLayout r2;
    LinearLayout r3;
    LinearLayout r4;
    LinearLayout r5;

    static ImageView dTY;
    static ImageView d1;
    static ImageView d2;
    static ImageView d3;
    static ImageView d4;
    static ImageView d5;

    static ImageView mTY;
    static ImageView m1;
    static ImageView m2;
    static ImageView m3;
    static ImageView m4;
    static ImageView m5;

    static private TextView pieniadzeTY;
    static private TextView pieniadze1;
    static private TextView pieniadze2;
    static private TextView pieniadze3;
    static private TextView pieniadze4;
    static private TextView pieniadze5;

    static TextView dealTY;
    static TextView deal1;
    static TextView deal2;
    static TextView deal3;
    static TextView deal4;
    static TextView deal5;

    static ImageView kart1;
    static ImageView kart2;
    static ImageView kart3;
    static ImageView kart4;

    static private Drawable drawable1;
    static private Drawable drawable2;
    static private Drawable drawable3;
    static private Drawable drawable4;
    static private Drawable drawable5;
    static private Drawable drawable6;
    static private Drawable drawable7;
    static private Drawable drawable8;
    static private Drawable drawable9;
    static private Drawable drawable10;
    static private Drawable drawable11;
    static private Drawable drawable12;
    static private Drawable drawable13;
    static private Drawable drawable14;
    static private Drawable drawable15;
    static private Drawable drawable16;
    static private Drawable drawable17;
    static private Drawable drawable18;
    static private Drawable drawable19;
    static private Drawable drawable20;
    static private Drawable drawable21;
    static private Drawable drawable22;
    static private Drawable drawable23;
    static private Drawable drawable24;
    static private Drawable drawable25;
    static private Drawable drawable26;
    static private Drawable drawable27;
    static private Drawable drawable28;
    static private Drawable drawable29;
    static private Drawable drawable30;
    static private Drawable drawable31;
    static private Drawable drawable32;
    static private Drawable drawable33;
    static private Drawable drawable34;
    static private Drawable drawable35;
    static private Drawable drawable36;
    static private Drawable drawable37;
    static private Drawable drawable38;
    static private Drawable drawable39;
    static private Drawable drawable40;
    static private Drawable drawable41;
    static private Drawable drawable42;
    static private Drawable drawable43;
    static private Drawable drawable44;
    static private Drawable drawable45;
    static private Drawable drawable46;
    static private Drawable drawable47;
    static private Drawable drawable48;
    static private Drawable drawable49;
    static private Drawable drawable50;
    static private Drawable drawable51;
    static private Drawable drawable52;

    static boolean fold = true;
    static boolean bet = true;
    static boolean check = true;
    static boolean call = true;
    static boolean all_in = true;
    protected int wartosc;

    int it1 = 0;
    int it2 = 0;
    int it3 = 0;
    int it4 = 0;

    static boolean wymien = false;
    int ilosc_graczy;
    String name;

    public void karta1(View view){

        if(wymien == true) {
            if (it1 == 0) {
                kart1.setBackgroundColor(Color.YELLOW);
                it1 = 1;
            } else {
                kart1.setBackgroundColor(Color.BLACK);
                it1 = 0;
            }
        }

    }

    public void karta2(View view){

        if(wymien == true) {
            if (it2 == 0) {
                kart2.setBackgroundColor(Color.YELLOW);
                it2 = 1;
            } else {
                kart2.setBackgroundColor(Color.BLACK);
                it2 = 0;
            }
        }

    }

    public void karta3(View view){

        if(wymien == true) {
            if (it3 == 0) {
                kart3.setBackgroundColor(Color.YELLOW);
                it3 = 1;
            } else {
                kart3.setBackgroundColor(Color.BLACK);
                it3 = 0;
            }
        }

    }

    public void karta4(View view){

        if(wymien == true) {
            if (it4 == 0) {
                kart4.setBackgroundColor(Color.YELLOW);
                it4 = 1;
            } else {
                kart4.setBackgroundColor(Color.BLACK);
                it4 = 0;
            }
        }

    }

    public void Check(View view){

    }

    public void Bet(View view){

    }

    public void Raise(View view){
        dialog(view);
    }

    public void Call(View view){

    }

    public void Fold(View view){

    }

    public void All_in(View view){

    }

    public void wymien_karty(View view){

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gra);

        TY = (TextView) findViewById(R.id.Ty);
        Gracz1 = (TextView) findViewById(R.id.gracz_1);
        Gracz2 = (TextView) findViewById(R.id.gracz_2);
        Gracz3 = (TextView) findViewById(R.id.gracz_3);
        Gracz4 = (TextView) findViewById(R.id.gracz_4);
        Gracz5 = (TextView) findViewById(R.id.gracz_5);

        lin1 = (LinearLayout) findViewById(R.id.Linear_1);
        lin2 = (LinearLayout) findViewById(R.id.Linear_2);
        lin3 = (LinearLayout) findViewById(R.id.Linear_3);
        lin4 = (LinearLayout) findViewById(R.id.Linear_4);
        lin5 = (LinearLayout) findViewById(R.id.Linear_5);

        r1 = (LinearLayout) findViewById(R.id.reka_1);
        r2 = (LinearLayout) findViewById(R.id.reka_2);
        r3 = (LinearLayout) findViewById(R.id.reka_3);
        r4 = (LinearLayout) findViewById(R.id.reka_4);
        r5 = (LinearLayout) findViewById(R.id.reka_5);

        dTY = (ImageView) findViewById(R.id.dcTY);
        d1 = (ImageView) findViewById(R.id.dc1);
        d2 = (ImageView) findViewById(R.id.dc2);
        d3 = (ImageView) findViewById(R.id.dc3);
        d4 = (ImageView) findViewById(R.id.dc4);
        d5 = (ImageView) findViewById(R.id.dc5);

        mTY = (ImageView) findViewById(R.id.mcTY);
        m1 = (ImageView) findViewById(R.id.mc1);
        m2 = (ImageView) findViewById(R.id.mc2);
        m3 = (ImageView) findViewById(R.id.mc3);
        m4 = (ImageView) findViewById(R.id.mc4);
        m5 = (ImageView) findViewById(R.id.mc5);

        pieniadzeTY = (TextView) findViewById(R.id.kasaTY);
        pieniadze1 = (TextView) findViewById(R.id.kasa1);
        pieniadze2 = (TextView) findViewById(R.id.kasa2);
        pieniadze3 = (TextView) findViewById(R.id.kasa3);
        pieniadze4 = (TextView) findViewById(R.id.kasa4);
        pieniadze5 = (TextView) findViewById(R.id.kasa5);

        dealTY = (TextView) findViewById(R.id.dealerTY);
        deal1 = (TextView) findViewById(R.id.dealer1);
        deal2 = (TextView) findViewById(R.id.dealer2);
        deal3 = (TextView) findViewById(R.id.dealer3);
        deal4 = (TextView) findViewById(R.id.dealer4);
        deal5 = (TextView) findViewById(R.id.dealer5);

        kart1 = (ImageView) findViewById(R.id.k1);
        kart2 = (ImageView) findViewById(R.id.k2);
        kart3 = (ImageView) findViewById(R.id.k3);
        kart4 = (ImageView) findViewById(R.id.k4);

        drawable1 = getResources().getDrawable(R.drawable.asdzwon);
        drawable2 = getResources().getDrawable(R.drawable.asser);
        drawable3 = getResources().getDrawable(R.drawable.astref);
        drawable4 = getResources().getDrawable(R.drawable.aswin);
        drawable5 = getResources().getDrawable(R.drawable.damaser);
        drawable6 = getResources().getDrawable(R.drawable.damatref);
        drawable7 = getResources().getDrawable(R.drawable.damawin);
        drawable8 = getResources().getDrawable(R.drawable.damdzwon);
        drawable9 = getResources().getDrawable(R.drawable.dzwon2);
        drawable10 = getResources().getDrawable(R.drawable.dzwon3);
        drawable11 = getResources().getDrawable(R.drawable.dzwon4);
        drawable12 = getResources().getDrawable(R.drawable.dzwon5);
        drawable13 = getResources().getDrawable(R.drawable.dzwon6);
        drawable14 = getResources().getDrawable(R.drawable.dzwon7);
        drawable15 = getResources().getDrawable(R.drawable.dzwon8);
        drawable16 = getResources().getDrawable(R.drawable.dzwon9);
        drawable17 = getResources().getDrawable(R.drawable.dzwon10);
        drawable18 = getResources().getDrawable(R.drawable.jopdzwon);
        drawable19 = getResources().getDrawable(R.drawable.jopser);
        drawable20 = getResources().getDrawable(R.drawable.joptref);
        drawable21 = getResources().getDrawable(R.drawable.jopwin);
        drawable22 = getResources().getDrawable(R.drawable.kroldzwon);
        drawable23 = getResources().getDrawable(R.drawable.krolser);
        drawable24 = getResources().getDrawable(R.drawable.kroltref);
        drawable25 = getResources().getDrawable(R.drawable.krolwin);
        drawable26 = getResources().getDrawable(R.drawable.ser2);
        drawable27 = getResources().getDrawable(R.drawable.ser3);
        drawable28 = getResources().getDrawable(R.drawable.ser4);
        drawable29 = getResources().getDrawable(R.drawable.ser5);
        drawable30 = getResources().getDrawable(R.drawable.ser6);
        drawable31 = getResources().getDrawable(R.drawable.ser7);
        drawable32 = getResources().getDrawable(R.drawable.ser8);
        drawable33 = getResources().getDrawable(R.drawable.ser9);
        drawable34 = getResources().getDrawable(R.drawable.ser10);
        drawable35 = getResources().getDrawable(R.drawable.tref2);
        drawable36 = getResources().getDrawable(R.drawable.tref3);
        drawable37 = getResources().getDrawable(R.drawable.tref4);
        drawable38 = getResources().getDrawable(R.drawable.tref5);
        drawable39 = getResources().getDrawable(R.drawable.tref6);
        drawable40 = getResources().getDrawable(R.drawable.tref7);
        drawable41 = getResources().getDrawable(R.drawable.tref8);
        drawable42 = getResources().getDrawable(R.drawable.tref9);
        drawable43 = getResources().getDrawable(R.drawable.tref10);
        drawable44 = getResources().getDrawable(R.drawable.win2);
        drawable45 = getResources().getDrawable(R.drawable.win3);
        drawable46 = getResources().getDrawable(R.drawable.win4);
        drawable47 = getResources().getDrawable(R.drawable.win5);
        drawable48 = getResources().getDrawable(R.drawable.win6);
        drawable49 = getResources().getDrawable(R.drawable.win7);
        drawable50 = getResources().getDrawable(R.drawable.win8);
        drawable51 = getResources().getDrawable(R.drawable.win9);
        drawable52 = getResources().getDrawable(R.drawable.win10);

        Bundle bundle = getIntent().getExtras();
        ID = bundle.getInt("ID");
        name = bundle.getString("name");
        TY.setText(name);
        gniazdo = MyActivity.gniazdo;
        strumienWejsciowy = MyActivity.strumienWejsciowy;
        strumienWyjsciowy = MyActivity.strumienWyjsciowy;
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    strumienWyjsciowy.writeObject("ilosc");
                    String message = (String) strumienWejsciowy.readObject();
                    ilosc_graczy = Integer.parseInt(message);
                    Log.d("ilosc", String.valueOf(ilosc_graczy));
                    strumienWyjsciowy.writeObject("kasa");
                    String message3 = (String) strumienWejsciowy.readObject();
                    if(ilosc_graczy == 6){
                        TY = (TextView) findViewById(R.id.Ty);
                        Gracz1 = (TextView) findViewById(R.id.gracz_1);
                        Gracz2 = (TextView) findViewById(R.id.gracz_2);
                        Gracz3 = (TextView) findViewById(R.id.gracz_3);
                        Gracz4 = (TextView) findViewById(R.id.gracz_4);
                        Gracz5 = (TextView) findViewById(R.id.gracz_5);

                        pieniadzeTY.setText(message3);
                        pieniadze1.setText(message3);
                        pieniadze2.setText(message3);
                        pieniadze3.setText(message3);
                        pieniadze4.setText(message3);
                        pieniadze5.setText(message3);

                /*TY.setText(g1);
                Gracz1.setText(g2);
                Gracz2.setText(g3);
                Gracz3.setText(g4);
                Gracz4.setText(g5);
                Gracz5.setText(g6);
                */


                    }
                    else if(ilosc_graczy == 5){
                        TY = (TextView) findViewById(R.id.Ty);
                        Gracz1 = (TextView) findViewById(R.id.gracz_1);
                        Gracz2 = (TextView) findViewById(R.id.gracz_2);
                        Gracz3 = (TextView) findViewById(R.id.gracz_3);
                        Gracz4 = (TextView) findViewById(R.id.gracz_4);

                        pieniadzeTY.setText(message3);
                        pieniadze1.setText(message3);
                        pieniadze2.setText(message3);
                        pieniadze3.setText(message3);
                        pieniadze4.setText(message3);

                /*TY.setText(g1);
                Gracz1.setText(g2);
                Gracz2.setText(g3);
                Gracz3.setText(g4);
                Gracz4.setText(g5);
                */

                        lin5.setVisibility(View.INVISIBLE);
                        r5.setVisibility(View.INVISIBLE);
                    }
                    else if(ilosc_graczy == 4){
                        TY = (TextView) findViewById(R.id.Ty);
                        Gracz1 = (TextView) findViewById(R.id.gracz_1);
                        Gracz2 = (TextView) findViewById(R.id.gracz_2);
                        Gracz3 = (TextView) findViewById(R.id.gracz_3);

                        pieniadzeTY.setText(message3);
                        pieniadze1.setText(message3);
                        pieniadze2.setText(message3);
                        pieniadze3.setText(message3);

                /*TY.setText(g1);
                Gracz1.setText(g2);
                Gracz2.setText(g3);
                Gracz3.setText(g4);
                */

                        lin5.setVisibility(View.INVISIBLE);
                        lin4.setVisibility(View.INVISIBLE);

                        r5.setVisibility(View.INVISIBLE);
                        r4.setVisibility(View.INVISIBLE);
                    }
                    else if(ilosc_graczy == 3){
                        TY = (TextView) findViewById(R.id.Ty);
                        Gracz1 = (TextView) findViewById(R.id.gracz_1);
                        Gracz2 = (TextView) findViewById(R.id.gracz_2);

                        pieniadzeTY.setText(message3);
                        pieniadze1.setText(message3);
                        pieniadze2.setText(message3);

                /*TY.setText(g1);
                Gracz1.setText(g2);
                Gracz2.setText(g3);
                */

                        lin5.setVisibility(View.INVISIBLE);
                        lin4.setVisibility(View.INVISIBLE);
                        lin3.setVisibility(View.INVISIBLE);

                        r5.setVisibility(View.INVISIBLE);
                        r4.setVisibility(View.INVISIBLE);
                        r3.setVisibility(View.INVISIBLE);
                    }
                    else if(ilosc_graczy == 2){
                        TY = (TextView) findViewById(R.id.Ty);
                        Gracz3 = (TextView) findViewById(R.id.gracz_3);
                        TextView textView = (TextView) findViewById(R.id.label_gracz_3);

                        pieniadzeTY.setText(message3);
                        pieniadze3.setText(message3);

                /*TY.setText(g1);
                Gracz3.setText(g2);
                textView.setText("Gracz 2");
                */

                        lin5.setVisibility(View.INVISIBLE);
                        lin4.setVisibility(View.INVISIBLE);
                        lin2.setVisibility(View.INVISIBLE);
                        lin1.setVisibility(View.INVISIBLE);

                        r5.setVisibility(View.INVISIBLE);
                        r4.setVisibility(View.INVISIBLE);
                        r2.setVisibility(View.INVISIBLE);
                        r1.setVisibility(View.INVISIBLE);
                    }
                    else if(ilosc_graczy == 1){
                        TY = (TextView) findViewById(R.id.Ty);
                        Gracz3 = (TextView) findViewById(R.id.gracz_3);
                        TextView textView = (TextView) findViewById(R.id.label_gracz_3);



                /*TY.setText(g1);
                Gracz3.setText(g2);
                textView.setText("Gracz 2");
                */

                        lin5.setVisibility(View.INVISIBLE);
                        lin4.setVisibility(View.INVISIBLE);
                        lin2.setVisibility(View.INVISIBLE);
                        lin1.setVisibility(View.INVISIBLE);

                        r5.setVisibility(View.INVISIBLE);
                        r4.setVisibility(View.INVISIBLE);
                        r2.setVisibility(View.INVISIBLE);
                        r1.setVisibility(View.INVISIBLE);
                    }
                    strumienWyjsciowy.writeObject("boty");
                    String message2 = (String) strumienWejsciowy.readObject();
                    int boty = Integer.parseInt(message2);
                    for (int i = 0; i < boty; i++) {
                        strumienWyjsciowy.writeObject("boty " + String.valueOf(i));
                        String bot_name = (String) strumienWejsciowy.readObject();
                        Log.d("bot name", bot_name);
                        int pozycja = ilosc_graczy-i;
                        Log.d("pozycja", String.valueOf(pozycja));

                        if (pozycja == 6){
                            Gracz5.setText(bot_name);
                        }
                        else if (pozycja == 5){
                            Gracz4.setText(bot_name);
                        }
                        else if (pozycja == 4){
                            Gracz3.setText(bot_name);
                        }
                        else if (pozycja == 3){
                            Gracz2.setText(bot_name);
                        }
                        else if (pozycja == 2){
                            Gracz1.setText(bot_name);
                        }

                    }

                    strumienWyjsciowy.writeObject("gracze");
                    String message4 = (String) strumienWejsciowy.readObject();
                    int gr = Integer.parseInt(message4);
                    int i = (ID + 1) % gr;
                    while (ID != i) {
                        strumienWyjsciowy.writeObject("gracze " + String.valueOf(i));
                        String message5 = (String) strumienWejsciowy.readObject();
                        if (i == 1) {
                            if (!message5.equals("brak")) {
                                Gracz1.setText(message5);
                                i = (ID + 1) % gr;
                            }
                        }
                        else if (i == 2) {
                            if (!message5.equals("brak")) {
                                Gracz2.setText(message5);
                                i = (ID + 1) % gr;
                            }
                        }
                        if (i == 3) {
                            if (!message5.equals("brak")) {
                                Gracz3.setText(message5);
                                i = (ID + 1) % gr;
                            }
                        }
                        if (i == 4) {
                            if (!message5.equals("brak")) {
                                Gracz4.setText(message5);
                                i = (ID + 1) % gr;
                            }
                        }
                        if (i == 5) {
                            if (!message5.equals("brak")) {
                                Gracz5.setText(message5);
                                i = (ID + 1) % gr;
                            }
                        }

                    }
                }
                catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();

        //new Odbieraj().execute();
    }

    public void dialog(View view){
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        final View Viewlayout = inflater.inflate(R.layout.dialog_box, (ViewGroup) findViewById(R.id.layout_dialog));
        final TextView item = (TextView) Viewlayout.findViewById(R.id.dialog_text_view);

        builder.setTitle("Dokonaj zmiany");
        builder.setView(Viewlayout);

        SeekBar seekBar = (SeekBar) Viewlayout.findViewById(R.id.dialog_seekbar);
        //seekBar.setMax(pokuj.maxi());
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.d("dialog", Integer.toString(progress));
                wartosc = progress;
                item.setText(Integer.toString(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int a = Integer.parseInt((String) item.getText());
                Log.d("watosc", Integer.toString(wartosc));
                //boolean t = pokuj.wlasne(Integer.parseInt(String.valueOf(wartosc)));
                //if(t == false)
                    //Toast.makeText(getApplicationContext(), "zamala stawka, sproboj jeszcze raz", Toast.LENGTH_SHORT).show();
                //else {
                    //pokuj.podjol_decyzje();
                    //pokuj.zmien();
                    bet = false;
                    check = false;
                    call = true;
            //    }
                //dialog.dismiss();
            }
        });
        builder.setNegativeButton("Anuluj", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.create();
        builder.show();
    }

    public class Odbieraj extends AsyncTask<Void, Void, View>{

        @Override
        protected View doInBackground(Void... params) {
            while (true){
                try {
                    strumienWyjsciowy.writeObject("gracze");
                    String wiadomosc = (String) strumienWejsciowy.readObject();
                    Thread.sleep(10000);
                    String w[] = wiadomosc.split(" ");
                    if (w[0].equals("Gracze")) {
                        int pozycja = 1;
                        /*for (int i = 0; i < i++){

                        }*/
                    }
                    for (String message: w){
                        if (message.equals("Gracze")){

                        }
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {

                }
            }
            //return null;
        }
    }

}
